/**
 * 用户信息计算与读写类
 */
package com.key;

import java.util.LinkedList;
import java.util.List;

import com.key.util.GameParams;
import com.key.util.UserSystemData;

public class KeyChineseChessUserInfoCaculate {
	 
	//用户信息类
	private KeyChineseChessUserInfo UserInfo = null;

	public KeyChineseChessUserInfo getUserInfo() {
		return UserInfo;
	}

	//用户读写类
	private KeyChineseChessUserInfoRW UserInfoRW = null;
	private List<String> listResult = new LinkedList<String>();//结果List
	
	private final static int RETRACT = 1;
	
	public int getUserRank() {
		return UserInfo.getUserRank();
	}
	
	public int getUserGrade() {
		return UserInfo.getUserGrade();
	}
	
	/**
	 * 初始化变量信息
	 */
	public void init() {
		if(UserInfo == null) {
			UserInfo = new KeyChineseChessUserInfo();
		}
		if(UserInfoRW == null) {
			UserInfoRW = new KeyChineseChessUserInfoRW();
			UserInfoRW.setUserInfoResultPath(UserSystemData.strResultPath);
		}
	}
	
	/**
	 * 读取用户信息
	 * @param strPath
	 */
	public void read(String strPath) {
		UserInfoRW.setUserInfoResultPath(strPath);
		GameParams.mainActivity.readUserInfoResultPath(listResult);
		
		initRead();
	}
	
	/**
	 * 读取结果初始化用户信息
	 */
	private void initRead() {
		if(listResult!=null&&listResult.size() == 7) {
			UserInfo.setUserGrade(Integer.parseInt(listResult.get(0)));
			UserInfo.setUserRank(Integer.parseInt(listResult.get(1)));
			UserSystemData.keyMusic.setMusicDegree(Integer.parseInt(listResult.get(2)));
			UserSystemData.keyMusic.setSingleDegree(Integer.parseInt(listResult.get(3)));
			if(Integer.parseInt(listResult.get(4)) == 1) {
				UserSystemData.keyMusic.setMusicChecked(true);
			} else {
				UserSystemData.keyMusic.setMusicChecked(false);
			}
			if(Integer.parseInt(listResult.get(5)) == 1) {
				UserSystemData.keyMusic.setSingleChecked(true);
			} else {
				UserSystemData.keyMusic.setSingleChecked(false);
			}
		} else {
			UserInfo.setUserRank(10);
			UserInfo.setUserGrade(9999);
			UserSystemData.keyMusic.setMusicDegree(5);
			UserSystemData.keyMusic.setSingleDegree(5);
			UserSystemData.keyMusic.setMusicChecked(true);
			UserSystemData.keyMusic.setSingleChecked(true);
		}
	}
	
	/**
	 * 用户信息写入文件
	 * @return
	 */
	public boolean write() {
		listResult.clear();
		listResult.add(Integer.toString(UserInfo.getUserGrade()));
		listResult.add(Integer.toString(UserInfo.getUserRank()));
		listResult.add(Integer.toString(UserSystemData.keyMusic.getMusicDegree()));
		listResult.add(Integer.toString(UserSystemData.keyMusic.getSingleDegree()));
		if(UserSystemData.keyMusic.getMusicChecked() == true) {
			listResult.add(Integer.toString(1));
		} else {
			listResult.add(Integer.toString(0));
		}
		if(UserSystemData.keyMusic.getSingleChecked() == true) {
			listResult.add(Integer.toString(1));
		} else {
			listResult.add(Integer.toString(0));
		}
		return GameParams.mainActivity.writeUserInfoResultPath(listResult);
	}
	
	/**
	 * 减分
	 * @param iGrade 分数
	 * @return
	 */
	public synchronized int inviteChess(int iGrade) {
			return UserInfo.subUserGrade(iGrade);
	}
	
	/**
	 * 加分
	 * @param score 分数
	 * @return
	 */
	public synchronized boolean addScore(int score)
	{
		return UserInfo.plusUserGrade(score);
	}
	
	public int retractChess() {
		return UserInfo.subUserGrade(RETRACT);
	}
}
